package com.hjq.base;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   : FragmentPagerAdapter 基类
 */
public class BaseFragmentAdapter<F extends Fragment> extends FragmentStatePagerAdapter {

    private List<F> mFragmentSet = new ArrayList<>(); // Fragment集合
    private FragmentManager fm;
    private F mCurrentFragment; // 当前显示的Fragment
    private boolean fragmentsUpdateFlag[] = {false, false, false, false, false};

    public BaseFragmentAdapter(FragmentActivity activity) {
        this(activity.getSupportFragmentManager());
    }

    public BaseFragmentAdapter(Fragment fragment) {
        this(fragment.getChildFragmentManager());

    }

    public BaseFragmentAdapter(FragmentManager manager) {
        super(manager);
        this.fm = manager;

    }

    @Override
    public int getItemPosition(Object object) {
        return BaseFragmentAdapter.POSITION_NONE;
    }

    @Override
    public F getItem(int position) {
        Log.e("tag", "getItem: "+position);
        return mFragmentSet.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentSet.size();
    }


    @Override
    public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        if (getCurrentFragment() != object) {
            // 记录当前的Fragment对象
            mCurrentFragment = (F) object;
        }
        super.setPrimaryItem(container, position, object);
    }

    public void addFragment(F fragment) {
        mFragmentSet.add(fragment);
    }

    public void setFragment(int posion, F fragment) {
        mFragmentSet.set(posion, fragment);
    }

    /**
     * 获取Fragment集合
     */
    public List<F> getAllFragment() {
        return mFragmentSet;
    }

    /**
     * 获取当前的Fragment
     */
    public F getCurrentFragment() {
        return mCurrentFragment;
    }

    public void setfiage(int posion) {
        fragmentsUpdateFlag[posion] = true;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //解决viewpager重复加载数据
    }


}